<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>商品列表</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="/${BasePath}/static/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="/${BasePath}/static/css/public.css" media="all" type="text/css">
</head>
<body class="childrenBody">
<form class="layui-form" lay-filter="example">
	<blockquote class="layui-elem-quote quoteBox">
			<div class="layui-inline">
				<div class="layui-input-inline">
					<input type="text" class="layui-input searchVal" placeholder="请输入内容" />
				</div>
				<a class="layui-btn search_btn" data-type="reload">搜索</a>
			</div>
			<div class="layui-inline">
				<a class="layui-btn layui-btn-normal editproducts_btn">添加商品</a>
			</div>
<!-- 			<div class="layui-inline">
				<a class="layui-btn layui-btn-danger layui-btn-normal delAll_btn">批量删除</a>
			</div> -->
	</blockquote>
	<table id="productsList" lay-filter="productsList"></table>
</form>
	<!--审核状态-->
	<script type="text/html" id="productsStatus">
		<span class="layui-red">{{d.productName}}</span>
	</script>

	<!--操作-->
	<script type="text/html" id="productsListBar">
		<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
		<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
		<a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="look">预览</a>
	</script>
<script src="/${BasePath}/static/layui/layui.js" charset="utf-8" type="text/javascript"></script>
<script type="text/javascript" src="/${BasePath}/static/js/common/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //新闻列表
    var tableIns = table.render({
        elem: '#productsList',
        url : '/${BasePath}/shops/queryProductContentPage',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        limits : [1,10,15,20,25],
        id : "productsListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'productName', title: '商品名称', width:150},
            {field: 'productType', title: '所属类别', align:'center'},
            {field: 'productPrice', title: '价格',  align:'center'},
            {field: 'productNumber', title: '数量', align:'center'},
            {field: 'browseNumber', title: '浏览量', align:'center'},
            {field: 'publisherState', title: '是否上架', align:'center', templet:function(d){
            	var checked = '' ;
            	if(d.publisherState=="是"){
            		checked = 'checked=""'
            	}
                return '<input type="checkbox"  name="'+d.id+'" lay-filter="newsTop" lay-skin="switch" lay-text="是|否" '+checked+'>'
            }}, 
            {field: 'createDate', title: '发布时间', align:'center', minWidth:110, templet:function(d){
                return d.createDate.substring(0,10);
            }},
            {title: '操作', width:170, templet:'#productsListBar',fixed:"right",align:"center"}
        ]]
    ,skin: 'line'
    });

    //是否置顶
    form.on('switch(newsTop)', function(data){
        var index = layer.msg('修改中，请稍候',{icon: 16,time:false,shade:0.8});
        setTimeout(function(){            
            if(data.elem.checked){
            	$.ajax({
                    type: 'post',
                    url: "/${BasePath}/shops/saveUpProduct",
                    data: {"id":data.elem.name,"publisherState":"1"},
                    dataType: 'json',
                    success: function(data){
                        if (data.code == "1"){
                        	layer.msg("上架成功！");
                        	layer.close(index);
                        }else{
                        	layer.msg(data.msg);
                        	layer.close(index);
                        }
                    }
                });
            }else{
            	$.ajax({
                    type: 'post',
                    url: "/${BasePath}/shops/saveUpProduct",
                    data: {"id":data.elem.name,"publisherState":"2"},
                    dataType: 'json',
                    success: function(data){
                        if (data.code == "1"){
                        	layer.msg("下架成功！");
                        	layer.close(index);
                        }else{
                        	layer.msg(data.msg);
                        	layer.close(index);
                        }
                    }
                });
            }
        },500);
    })

    $(".search_btn").on("click",function(){
        if($(".searchVal").val() != ''){
            table.reload("productsListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                	productName: $(".searchVal").val()  //搜索的关键字
                }
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //编辑商品
    function editproducts(edit){
    	   window.location.href="/${BasePath}/shops/addProduct?id="+edit.id;
    }
    $(".editproducts_btn").click(function(){
    	window.location.href="/${BasePath}/shops/addProduct"; 
    })

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('productsListTable'),
            data = checkStatus.data,
            productsId = [];
        if(data.length > 0) {
            for (var i in data) {
                productsId.push(data[i].productsId);
            }
            layer.confirm('确定删除选中的商品？', {icon: 3, title: '提示信息'}, function (index) {
                // $.get("删除商品接口",{
                //     productsId : productsId  //将需要删除的productsId作为参数传入
                // },function(data){
                tableIns.reload();
                layer.close(index);
                // })
            })
        }else{
            layer.msg("请选择需要删除的商品");
        }
    })

    //列表操作
    table.on('tool(productsList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            editproducts(data);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此商品？',{icon:3, title:'提示信息'},function(index){
                 $.get("/${BasePath}/shops/deleteProduct",{
                     id : data.id  //将需要删除的productsId作为参数传入
                 },function(data){
                    tableIns.reload();
                    layer.close(index);
                 })
            });
        } else if(layEvent === 'look'){ //预览
        	/* window.open("/${BasePath}/free/productDetial?id="+data.id);  */
        	layer.msg("功能尚未开放")
        }
    });

})
</script>
</body>
</html>